/*
 * @(#) $Header$
 *
 * Copyright (C) 2010  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.crypto.md;


/**
 * Class {@code MDFamily} is the parent of the MD message digests.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.crypto.md.MDFamily">Daniel Léonard</a>
 * @version $Revision$
 */
public abstract class MDFamily extends AbstractBlockMessageDigest {

//---------------------------
// Class variable
//---------------------------

   /** The length in bytes of a block. */
    public static final int BUFFER_LEN = 64;


//---------------------------
// Instance variable
//---------------------------

    /** The internal state. */
    private int[] state;


//---------------------------
// Constructor
//---------------------------

  /**
   * Constructor.
   * @param   name   the name of the algorithm
   * @param   digest_len   the size of the digest in bytes.
   * @param   state_len   the size of the internal state in blocks of 32 bits.
   */
   @SuppressWarnings("hiding")
   protected MDFamily(String name, int digest_len, int state_len) {
      super(name, MDFamily.BUFFER_LEN, digest_len);

      int[] state = new int[state_len];
      this.setState(state);
      }


//---------------------------
// Accessor and mutator
//---------------------------

   /**
    * Completely changes the internal state.
    * @param   state   the new state.
    */
   protected void setState(int[] state) {
      this.state = state;
      }

   /**
    * Gets the internal state.
    * @return   the internal state.
    */
   protected int[] getState() {
      return this.state;
      }


//---------------------------
// Implemented methods from fork.crypto.AbstractBlockMessageDigest
//---------------------------

   @Override
   @SuppressWarnings("hiding")
   protected byte[] coreDoFinal() {
      long count = this.getCount();
      long bits = count * 8L;

      int buffer_len = this.getBufferLen();
      int index = (int) (count % buffer_len);
      int padlen = (index < (buffer_len - 8)) ? (buffer_len - 8) - index : (2 * buffer_len - 8) - index;
      byte[] last = new byte[padlen + 8];

      last[0] = (byte) 0x80;
      padlen = AbstractBlockMessageDigest.breakLongLE(bits, last, padlen);

      this.engineUpdate(last, 0, padlen);

      int digest_len = this.getDigestLen();
      byte[] digest = new byte[digest_len];
      int[] state = this.getState();
      for (int i = 0, j = 0; j < digest_len; i++) {
         j = AbstractBlockMessageDigest.breakIntLE(state[i], digest, j);
         }

      this.coreInit();
      return digest;
      }


//---------------------------
// Implemented method from java.lang.Cloneable
//---------------------------

   /**
    * Creates a deep copy.
    * @return   a deep copy.
    */
   @Override
   public Object clone() throws CloneNotSupportedException {
       MDFamily md = (MDFamily) super.clone();
       md.state = this.state.clone();
       return md;
       }

   }

